<?php
// +----------------------------------------------------------------------
// | flow-notify / Chuanglan.php    [ 2021/11/9 10:40 上午 ]
// +----------------------------------------------------------------------
// | Copyright (c) 2011~2021 zhangqiquan All rights reserved.
// +----------------------------------------------------------------------
// | Author: zhangqiquan <768617998@qq.com>
// +----------------------------------------------------------------------
declare (strict_types=1);

namespace notify\sms;

class Chuanglan
{
    protected $config = [
        'send_url' => 'http://smssh1.253.com/msg/send/json', // 短信发送地址
        'balance_url' => 'http://smssh1.253.com/msg/balance/json', // 短信余额查询地址
        'account' => '', // 账户
        'password' => '', // 密码
        'report' => true, // 是否需要状态报告
    ];

    /**
     * 构造器
     * Chuanglan constructor.
     * @param array $config
     */
    function __construct(array $config = [])
    {
        $this->config = array_merge($this->config, $config);
    }

    /**
     * 发送短信
     * @param string $phone
     * @param string $msg
     * @return mixed
     * @throws \Exception
     */
    public function send(string $phone, string $msg){
        return $this->curlPost($this->config['send_url'], [
            'account' => $this->config['account'],
            'password' => $this->config['password'],
            'msg' => urlencode($msg),
            'phone' => $phone,
            'report' => $this->config['report']
        ]);
    }

    /**
     * 查询短信余额
     * @return mixed
     * @throws \Exception
     */
    public function balance(){
        //查询参数
        $result = $this->curlPost($this->config['balance_url'], [
            'account' => $this->config['account'],
            'password' => $this->config['password'],
        ]);
        return $result;
    }

    /**
     * 通过CURL发送HTTP请求
     * @param string $url  //请求URL
     * @param array $postFields //请求参数
     * @return mixed
     */
    private function curlPost(string $url, array $postFields){
        $postFields = json_encode($postFields);
        $ch = curl_init ();
        curl_setopt( $ch, CURLOPT_URL, $url );
        curl_setopt( $ch, CURLOPT_HTTPHEADER, array(
                'Content-Type: application/json; charset=utf-8'   //json版本需要填写  Content-Type: application/json;
            )
        );
        curl_setopt($ch, CURLOPT_IPRESOLVE, CURL_IPRESOLVE_V4); //若果报错 name lookup timed out 报错时添加这一行代码
        curl_setopt( $ch, CURLOPT_RETURNTRANSFER, 1 );
        curl_setopt( $ch, CURLOPT_POST, 1 );
        curl_setopt( $ch, CURLOPT_POSTFIELDS, $postFields);
        curl_setopt( $ch, CURLOPT_TIMEOUT,10);
        curl_setopt( $ch, CURLOPT_SSL_VERIFYHOST, 0);
        curl_setopt( $ch, CURLOPT_SSL_VERIFYPEER, 0);
        $ret = curl_exec ( $ch );
        if (false == $ret) {
            throw new \Exception(curl_error(  $ch), 500);
        } else {
            $rsp = curl_getinfo( $ch, CURLINFO_HTTP_CODE);
            if (200 != $rsp) {
                throw new \Exception($url.';请求:'.$postFields.';错误:'.$rsp.';结果:'.$ret, 500);
            } else {
                $result = $ret;
            }
        }
        curl_close ( $ch );
        $output = json_decode($result, true);
        if($output) $result = $output;
        return $result;
    }
}